function preorderTraversal(root) {
    let stack = []
    let current = null
    stack.push(root)
    while (stack.length > 0) {
        current = stack.pop()
        if (current.right !== null) {
            stack.push(current.right)
        }
        if (current.left !== null) {
            stack.push(current.left)
        }
        console.log(current.value);
    }
}

function inorderTraversal(root) {
    let stack = []
    let current = null
    stack.push(root)
    pushLeft(root)
    function pushLeft(node) {
        while (node.left !== null) {
            stack.push(node.left)
            node = node.left
        }
    }
    while (stack.length > 0) {
        current = stack.pop()
        if (current.right !== null) {
            stack.push(current.right)
            pushLeft(current.right)
        }
        console.log(current.value);
    }
}

function postorderTraversal(root) {
    let stack1 = []
    let stack2 = []
    let current = null
    stack1.push(root)
    while (stack1.length > 0) {
        current = stack1.pop()
        if (current.left !== null) {
            stack1.push(current.left)
        }
        if (current.right !== null) {
            stack1.push(current.right)
        }
        stack2.push(current.value)
    }
    while (stack2.length > 0) {
        console.log(stack2.pop());
    }
}

function node(value) {
    this.right = null
    this.left = null
    this.value = value
}

function creatTree(root) {
    root = new node(4)
    root.left = new node(3)
    root.right = new node(2)
    root.left.right = new node(1)
    root.right.left = new node(5)
    root.right.right = new node(3)
    return root
}

let node1 = null
let root = creatTree(node1)
preorderTraversal(root)